package entity;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

public class MovingEntity extends LivingEntity{

	public Vector2f target = new Vector2f();
	public Vector2f movement = new Vector2f();
	public Vector2f lastMovement = new Vector2f();
	public Vector2f lastPosition = new Vector2f();
   
	public float speed;
	
	public float getSpeed() {
		return speed;
	}
	public MovingEntity(Vector2f position)
	{
		super(position);
	}
	public double calculateTheta() {
		return movement.getTheta();
	}
     
     public Vector2f calculateMovement() {
    	 movement.set(target);
    	 movement.sub(position).normalise().scale(speed);
                    
    	 double newTheta = movement.getTheta();
    	 double lastTheta = lastMovement.getTheta();
	             
    	 double thetaDif = lastTheta - newTheta;
    	 double step = stepScale();
             
    	 if(Math.abs(thetaDif) < step || Math.abs(thetaDif) > 360 - step)
    		 return movement;
             
    	 if(thetaDif > 180)
    		 thetaDif = thetaDif - 360;
    	 else if(thetaDif < -180)
    		 thetaDif = thetaDif + 360;
             
    	 step = thetaDif > 0 ? step : -step;
             
    	 movement.setTheta(lastTheta - step);
             
    	 return movement;
     }
     
	protected float stepScale() {
		/*if(target.distance(position) < 50)
			return 5;
		else*/
		if(speed < 1)
			return 2;
			return speed * 2;
	}
	public void update(GameContainer container, int delta) throws SlickException {
             

    }
	public void move()
	{
		movement = calculateMovement();
		lastPosition = position;
   	 	position.add(movement);
   	 	lastMovement.set(movement);
	}
}
